﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class InPatientsBLL
    {
        HospitalDataContext DB = new HospitalDataContext();
        public string Insert(InPatientsDTO nhap)
        {
            try
            {
                InPatient moi = new InPatient();
                moi.ListID = nhap.ListID;
                moi.ExpectedStay = nhap.ExpectedStay;
                moi.DatePlaced = nhap.DatePlaced;
                moi.DateLeave = nhap.DateLeave;
                //moi.ActualLeave = nhap.ActualLeave;
                moi.BedNumber = nhap.BedNumber;

                DB.InPatients.InsertOnSubmit(moi);
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception ee)
            {
                return ee.Message;
            }
        }
        public IQueryable SearchwithName(string name)
        {
            var kqs = (from inpatient in DB.InPatients
                       join listw in DB.ListWaits
                       on inpatient.ListID equals listw.ListID into newlistws
                       from newlistw in newlistws.DefaultIfEmpty()
                       join appoint in DB.Appointments
                       on newlistw.AppointmentID equals appoint.AppointmentID into las
                       from la in las.DefaultIfEmpty()
                       join pa in DB.Patients
                       on la.PatientID equals pa.PatientID into lpas
                       from lpa in lpas.DefaultIfEmpty()
                       select new
                       {
                           lpa.PatientID,
                           Name=lpa.FirstName+" "+lpa.LastName,
                           newlistw.DateRegistered,
                           inpatient.ExpectedStay,
                           inpatient.DatePlaced,
                           inpatient.DateLeave,
                           inpatient.ActualLeave,
                           inpatient.BedNumber,
                           newlistw.WardID,
                           newlistw.ListID

                       }
                   ).Distinct().Where(moi=>moi.Name.Contains(name));
            return kqs;
        }
        public IQueryable Search_PatientCurrent_withWardID(string name)
        {
            var kqs = (from inpatient in DB.InPatients
                       join listw in DB.ListWaits
                       on inpatient.ListID equals listw.ListID into newlistws
                       from newlistw in newlistws.DefaultIfEmpty()
                       join appoint in DB.Appointments
                       on newlistw.AppointmentID equals appoint.AppointmentID into las
                       from la in las.DefaultIfEmpty()
                       join pa in DB.Patients
                       on la.PatientID equals pa.PatientID into lpas
                       from lpa in lpas.DefaultIfEmpty()
                       where newlistw.WardID==name && newlistw.Status=="inpa" && (inpatient.ActualLeave==null || inpatient.ActualLeave > DateTime.Now)
                       select new
                       {
                           lpa.PatientID,
                           Name = lpa.FirstName + " " + lpa.LastName,
                           newlistw.DateRegistered,
                           inpatient.ExpectedStay,
                           inpatient.DatePlaced,
                           inpatient.DateLeave,
                           inpatient.ActualLeave,
                           inpatient.BedNumber,
                           newlistw.WardID,
                           newlistw.ListID

                       }
                   ).Distinct(); // (moi.ActualLeave==null || moi.ActualLeave > DateTime.Now) );
            Console.WriteLine("tim benh nhan hien tai"+kqs.Count());
            return kqs;
            
        }
        public InPatient Get(string id)
        {
            return DB.InPatients.Where(bang => bang.ListID == id).FirstOrDefault();
        }
        public string Update(InPatientsDTO nhap)
        {
            try
            {
                InPatient moi = DB.InPatients.Where(inpa => inpa.ListID == nhap.ListID).FirstOrDefault();
                moi.BedNumber = nhap.BedNumber;
                moi.ActualLeave = nhap.ActualLeave;
                DB.SubmitChanges();
                return "success";
            }
            catch(Exception ee)
            {
                return ee.Message;
            }
        }
        public void Update_patient_out()
        {
            try
            {
                var kqs = DB.InPatients.Join(
                        DB.ListWaits,
                        a => a.ListID,
                        b => b.ListID,
                        (a, b) => new
                        {
                            a.ListID,
                            a.BedNumber,
                            a.ActualLeave,
                            b.WardID,                        
                        }
                    ).Where(moi => moi.ActualLeave!=null && moi.ActualLeave < DateTime.Now);
                BedsBLL bed = new BedsBLL();
                foreach (var kq in kqs)
                {
                    BedsDTO nhap = new BedsDTO();
                    nhap.WardID = kq.WardID;
                    nhap.BedID = kq.BedNumber;
                    bed.Update_when_patient_leave(nhap);
                }
               
            }
            catch (Exception ee)
            { }
        }
    }
}
